<template>
  <view>
    <my-search @myclick="handel"></my-search>
    <view class="view-container">
      <scroll-view class="left-scroll-view" scroll-y="true" :style="{height: wh+'px'}">
        <block  v-for="(item,index) in cateList">
          <view class="item" :class="{active:active == index}" :key="index" @click="changeHandel(index)">{{item.cat_name}}</view>
        </block>
        
      </scroll-view>
      <scroll-view class="right-scroll-view" scroll-y="true" :scroll-top="scrollTop" :style="{height: wh+'px'}">
        <view class="right-item" v-for="(item,index) in cateList2" :key="index">
          <view class="right-item-title">/{{item.cat_name}}/</view>
          <view class="right-item-box">
            <view v-for="(item2,i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
              <image :src="item2.cat_icon"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import badgeMix from '@/mixins/tabber-badge.js'
  export default {
    mixins:[badgeMix],
    data() {
      return {
        //屏幕可用高度
        wh:0,
        cateList:[],
        active:0,
        //二级分类
        cateList2:[],
        scrollTop:0
      }
    },
    onLoad() {
      //获取可用屏幕高度
      const info = uni.getSystemInfoSync()
      this.wh=info.windowHeight - 50
      this.getCateList()
    },
    methods:{
      async getCateList(){
        const {data:res} = await uni.$http.get('/api/public/v1/categories')
        if(res.meta.status!==200)return uni.$showMsg()
        this.cateList = res.message
        this.cateList2 = res.message[0].children
      },
      changeHandel(index){
        this.active = index
        this.cateList2 = this.cateList[index].children
        //如果每次值一样 scroll-top是不生效的 所以在0和1之间
        this.scrollTop = this.scrollTop == 0?1:0
      },
      gotoGoodsList(item){
        uni.navigateTo({
          url:'/subpkg/goods_list/goods_list?cid='+item.cat_id
        })
      },
      handel(){
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .view-container{
    display: flex;
    .left-scroll-view{
      width:120px;
      .item{
        background:#f7f7f7;
        font-size: 12px;
        text-align:center;
        line-height:60px;
        &.active{
          background:#fff;
          position: relative;
          &::before{
            content: ' ';
            width:3px;
            height:30px;
            background:red;
            position:absolute;
            top:50%;
            left:0;
            transform: translateY(-50%);
          }
        }
      }
    }
  }
  .right-item-title{
    font-size: 14px;
    font-weight: bold;
    text-align: center;
    padding :15px 0;
  }
  .right-item-box{
    display: flex;
    flex-wrap: wrap;
    view{
      width:33.33%;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-bottom: 10px;
      image{
        width:60px;
        height:60px;
      }
      text{
        font-size: 12px;
      }
    }
  }
</style>
